/**********************************************************************
 Copyright (C) 2010 Teppo Eeva

 This file is part of Arithmetic Game.
 For more information, see <http://code.google.com/p/arithmetic-game/>

 Arithmetic Game is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Arithmetic Game is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Arithmetic Game.  If not, see <http://www.gnu.org/licenses/>.
 **********************************************************************/

#include "highscoreitem.h"
#include <QTime>
#include <QCryptographicHash>

HighScoreItem::HighScoreItem(QObject *parent):
    QObject(parent)
{
}

HighScoreItem::HighScoreItem(QString name, int score, int level, QByteArray uniqueHash):
        m_name(name),
        m_score(score),
        m_level(level),
        m_uniqueHash(uniqueHash)
{
}

HighScoreItem::HighScoreItem(QString name, int score, int level):
        m_name(name),
        m_score(score),
        m_level(level)
{
    generateUniqueHash();
}

HighScoreItem::HighScoreItem(const HighScoreItem &other)
{
    m_name = other.m_name;
    m_score = other.m_score;
    m_level = other.m_level;
    m_uniqueHash = other.m_uniqueHash;
}

HighScoreItem::~HighScoreItem()
{
    generateUniqueHash();
}

HighScoreItem& HighScoreItem::operator=(const HighScoreItem &other)
{
    m_name = other.m_name;
    m_score = other.m_score;
    m_level = other.m_level;
    m_uniqueHash = other.m_uniqueHash;
    return *this;
}

bool HighScoreItem::operator==(const HighScoreItem &other)
{
   return ( other.m_name == m_name &&
            other.m_score == m_score &&
            other.m_name == m_name &&
            other.m_uniqueHash == m_uniqueHash);
}

bool HighScoreItem::operator!=(const HighScoreItem &other)
{
   return ( !(*this == other));
}


void HighScoreItem::generateUniqueHash()
{
    QCryptographicHash hash(QCryptographicHash::Md5);
    hash.addData( m_name.toAscii() );
    hash.addData( QByteArray::number(m_score) );
    hash.addData( QByteArray::number(m_level) );
    QString time = QTime::currentTime().toString();
    hash.addData( time.toAscii() );
    m_uniqueHash = hash.result();
}

const QByteArray& HighScoreItem::uniqueHash()
{
    return m_uniqueHash;
}

QDebug operator<<(QDebug dbg, const HighScoreItem &item)
{
    dbg.nospace() << item.m_name << ", " << item.m_score << ", " << item.m_level << ", " << item.m_uniqueHash;
    return dbg.space();
}
